#include "keydialog.h"
#include <QDebug>

KeyDialog::KeyDialog(QWidget * parent) : KeyControllableMenu(parent)
{
    setLayout(new QVBoxLayout());
    label = new QLabel("Please enter the desired key combination");
    QWidget * mainWidget=new QWidget(this);
    mainWidget->setLayout(new QVBoxLayout());
    mainWidget->layout()->addWidget(label);
    mainWidget->setObjectName("testWidget");
    input = new QLabel(this);
    //label->setStyleSheet("text-align: center");
    mainWidget->layout()->addWidget(input);
    oneKey=false;
    //mainWidget->setStyleSheet("");
    setStyleSheet("KeyControllableMenu { background: rgb(0, 0, 0); color: rgb(255,255,255); } .QWidget {border: 1px solid white; border-radius: 10px;}");
    //setStyleSheet(".QWidget{border: 1px solid white; border-radius: 10px; background: rgba(0, 0, 0,255); color: rgb(255,255,255);} KeyControllableMenu { background: rgb(0, 0, 0); color: rgb(255,255,255); };");
    okButton=new Button(QIcon(""),"Ok",this);
    okButton->setMaximumWidth(100);
    cancelButton=new Button(QIcon(""),"Cancel",this);
    cancelButton->setMaximumWidth(100);
    QList<Button *> * list=new QList<Button *>();
    list->append(okButton);
    list->append(cancelButton);
    addRow(list);
    QHBoxLayout * horizontalLayout=new QHBoxLayout();
    horizontalLayout->addWidget(okButton);
    horizontalLayout->addWidget(cancelButton);
    mainWidget->layout()->addItem(horizontalLayout);
    setMinimumSize(400,300);
    horizontalLayout->setSpacing(20);
    layout()->addWidget(mainWidget);
}

bool KeyDialog::isOneKey() {
    return oneKey;
}

void KeyDialog::setOneKey(bool value) {
    oneKey = value;
    if(oneKey) {
        input->hide();
        okButton->hide();
        cancelButton->hide();
    }
}

QString KeyDialog::getText() {
    return input->text();
}

int KeyDialog::getModifiers() {
    return modifiers;
}

void KeyDialog::show() {
    grabKeyboard();
    QWidget::show();
}

void KeyDialog::setKey(int key, int modifiers) {
    input->setText(QString::number(key));
    this->modifiers=modifiers;
}

void KeyDialog::keyPressEvent(QKeyEvent * event) {
    qDebug() <<"keydialog "<<(event->key())<<" "<<(event->modifiers());
    //KeyControllableMenu::keyPressEvent(event);
    if(event->key()==0||event->key()==Qt::Key_Shift||event->key()==Qt::Key_Alt||event->key()==Qt::Key_Control||event->key()==Qt::Key_Meta) {

    }
    else if(oneKey) {
        modifiers=event->modifiers();
        QString textValue=""+QString::number(event->key());
        qDebug() <<textValue;
        input->setText(textValue);
        hide();
        releaseKeyboard();
        emit okSelected(this);
    } else {
        if(event->key()==Qt::Key_Backspace) {
            input->setText(input->text().mid(0,input->text().size()-1));
        }
        else {
            input->setText(input->text()+event->text());
        }

    }
}

void KeyDialog::closeEvent(QCloseEvent *event) {
    qDebug() <<"closing dialog";
    QWidget::closeEvent(event);
    input->setText("");
    emit cancelSelected();
}

void KeyDialog::setLabel(QString label) {
    this->label->setText(label);
}

void KeyDialog::buttonSelected() {
    hide();
    if(getCurrentButton()==okButton) {
        emit okSelected(this);
    } else {
        input->setText("");
        emit cancelSelected();
    }
}

Button * KeyDialog::getOkButton() {
    return okButton;
}

Button * KeyDialog::getCancelButton() {
    return cancelButton;
}

void KeyDialog::clearText() {
    input->clear();
}
